// pages/wode/wode.js
const app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xt_tj1: "color:#fff;background:#0b5fff",
    xt_tj2: "color:#0b5fff;background:none",
    yc_1: "display:block",
    yc_2: "display:none",
    isHide: false,
    AuthorizedLogin: '授权登录',
    UserPhone: '手机号授权',
    lee: "",
    flag: true,
    index_sj: "",
    shequ: "",
    sq_l: "display:none",
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    libiao: '',
    countdown: '',
    listData: [],
    currentUrlIndex: 0,
    articles: [],
    hidden: true,
    loadingData: false,
    show: false,
    tui_j: [],
    statusBarHeight: getApp().globalData.statusBarHeight,
		statusBarHeight1: getApp().globalData.statusBarHeight1,
    status1: '',
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    ht_num:1,
    navScrollLeft: 0,
    winWidth: 0,
    winHeight: 0, 
    currentTab:0
  },
  interpretatione(){
    wx.navigateTo({
      url: '../../classfiy/interpretation/interpretation',
    })
  },
  information(){
    wx.navigateTo({
      url: '../../classfiy/information/information',
    })
  },
  referral(e){
    console.log(e)
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../../classfiy/referral/referral?id='+id,
    })
  },
  information_xq(e){
    console.log(e)
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../../classfiy/information_xq/information_xq?id='+id,
    })
  },
  Case_details(e){
    console.log(e)
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../../classfiy/Case_details/Case_details?id='+id,
    })
  },
  lawyer(){
    wx.navigateTo({
      url: '../../classfiy/lawyer/lawyer',
    })
  },
  servicexq(e){
    console.log(e)
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../classfiy/servicexq/servicexq',
    })
  },
  service(){
    wx.navigateTo({
      url: '../../classfiy/service/service',
    })
  },
  city(){
    wx.navigateTo({
      url: '../../classfiy/city/city',
    })
  },
  classfiy(){
    wx.navigateTo({
      url: '../../classfiy/classfiy/classfiy',
    })
  },
  // 更多模板
  moban(){
    wx.navigateTo({
      url: '../moban/moban',
    })
  },
  switchNav(event){
      var that = this;
      var cur = event.currentTarget.dataset.current; 
      var singleNavWidth = this.data.windowWidth / 5;
      this.setData({
          navScrollLeft: (cur - 2) * singleNavWidth
      })      
      if (this.data.currentTab == cur) {
          return false;
      } else {
          this.setData({
              currentTab: cur
          })
      }
  },
  gr_xx: function () {
    if (app.globalData.sf_zs == '' || app.globalData.sf_zs == null || app.globalData.sf_zs == undefined) {
      wx.showModal({
        title: '温馨提示',
        content: '您还没有实名认证，是否要去认证？',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../shimingrenzheng/shimingrenzheng',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '../gr_xx/gr_xx',
      })
    }

  },
  loadData: function (tail, callback) {
    if (app.globalData.lishi == undefined) {
      app.globalData.lishi = 0
    }
    var that = this,
      urlIndex = that.data.currentUrlIndex;
    if (urlIndex == 0) {
      urlIndex = 1
    }
    var shul = 20 * 1 * urlIndex * 1
    console.log(shul)
    app.globalData.shul = shul
    var that = this
    wx.request({
      url: app.globalData.url_l + 'api/member/moban_list',
      method: 'post',
      data: {
        uid: app.globalData.uid,
        state: app.globalData.shu,
        shu: app.globalData.shul,
        lei: '',
        content: ""
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(222222222)
        console.log(res)
        if (res.data.data.length == 0) {
          wx.showToast({
            title: "没有更多数据！",
            icon: 'none',
            duration: 2000
          })
        }
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (res.data.shi == "") {
          that.setData({
            meiyougengdu: "display:block"
          })
        }
        var oldArticles = that.data.tui_j,
          newArticles = tail ? oldArticles.concat(res.data.data) : res.data.data;
        app.globalData.cd_id1 = newArticles.length
        var shi_dj = [];
        app.globalData.po = (urlIndex + 1) >= res.data.data.length ? 0 : urlIndex + 1
        console.log(newArticles)
        that.setData({
          tui_j: newArticles,
          currentUrlIndex: (urlIndex + 1) >= res.data.data.length ? 0 : urlIndex + 1,
          sj_xz: "display:none",
          sj_xz1: "display:none",
        });
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (callback) {
          callback();
        }
      },
    })
    // }
  },

  // /**
  //  * 监听用户下拉动作
  //  */
  onPullDownRefresh: function () {
    var that = this
    wx.request({
      url: app.globalData.url_l + 'api/member/moban_list',
      method: 'post',
      data: {
        uid: app.globalData.uid,
        state: 1,
        shu: 0,
        lei: '',
        content: ""
      },
      timeout: 3000,
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(11111111)
        console.log(res)
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        // app.globalData.huancun = res.data.list
        that.setData({
          tui_j: res.data.data
        })
        //that.setCountDown();

      },
    })

    wx.showToast({
      title: "数据加载中...",
      icon: 'loading',
      duration: 2000
    })

  },
  onReachBottom: function () {
    console.log("触底了");
    console.info('onReachBottom');
    var hidden = this.data.hidden,
      loadingData = this.data.loadingData,
      that = this;
    if (hidden) {
      this.setData({
        hidden: false
      });
      //console.info(this.data.hidden);
    }
    if (loadingData) {
      return;
    }
    this.setData({
      loadingData: true
    });
    // 加载数据,模拟耗时操作
    wx.showToast({
      title: "数据加载中...",
      icon: 'loading',
      duration: 2000
    });

    setTimeout(function () {
      that.loadData(true, () => {
        that.setData({
          hidden: true,
          loadingData: false
        });
      });
      console.info('上拉数据加载完成.');
    }, 1000);
  },
  xt_tj1: function (e) {
    var that = this;

    app.globalData.shu = e.currentTarget.dataset.index==1?'1':app.globalData.corpid;
    this.setData({
      ht_num:e.currentTarget.dataset.index
    })
    wx.request({
      url: app.globalData.url_l + 'api/member/moban_list',
      method: 'post',
      data: {
        uid: app.globalData.uid,
        state: app.globalData.shu,
        shu: 0,
        lei: '',
        content: ""
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          tui_j: res.data.data
        })
      }
    })
  },
  xt_tj2: function () {
    var that = this;
    app.globalData.shu = app.globalData.corpid
    wx.request({
      url: app.globalData.url_l + 'api/member/moban_list',
      method: 'post',
      data: {
        uid: app.globalData.uid,
        state: app.globalData.shu,
        shu: 0,
        lei: '',
        content: ""
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          tui_j: res.data.data
        })
      }
    })
  },
  ht: function (e) {
    var that = this;
    var he_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../moban_doc/moban_doc?mb_id=' + he_id,
    })
  },

  daichuli: function (e) {
    var sq_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../hetong1/hetong1?sq_id=' + sq_id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onlaunch: function (e) {


  },
  index() {
    var that = this;
    wx.request({
      url: app.globalData.url_l + 'api/member/moban_list',
      method: 'post',
      data: {
        uid: app.globalData.uid,
        state: 1,
        shu: 0,
        lei: '',
        content: "",
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          tui_j: res.data.data
        })
      }
    })
    wx.request({
      url: app.globalData.url_l + 'api/member/index',
      method: 'post',
      data: {
        uid: app.globalData.uid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        console.log(app.globalData.uid)
        if (res.data.code == 1) {
          app.globalData.corpid = res.data.data.corpid
          app.globalData.sf_zs = res.data.data.idnum
          that.setData({
            ge_xx: res.data.data,
            taxs: res.data
          })
        } else {
          that.setData({
            ge_xx: '',
            taxs: ''
          })
        }
      }
    })
  },
  onLoad: function (options) {
      var that = this;
      wx.getSystemInfo({
        success: (res) => {
            this.setData({
                pixelRatio: res.pixelRatio,
                windowHeight: res.windowHeight,
                windowWidth: res.windowWidth
            })
        },
    }) 
  },


  sq_login: util.throttle(function (e) {
    var that = this;
    wx.getUserProfile({
      desc: "获取你的昵称、头像、地区及性别",
      success: res => {
        console.log(res)
        let user = res.userInfo;
        that.setData({
          avatarUrl: user.avatarUrl
        })
        wx.login({
          success: function (data) {
            var postData = {
              code: data.code
            };
            wx.request({
              url: app.globalData.url_l + 'api/login/member_open',
              method: 'post',
              data: postData,
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                console.log(res)
                app.globalData.openid = res.data.openid.openid
                
                wx.request({
                  url: app.globalData.url_l + 'api/login/login',
                  method: 'post',
                  data: {
                    openid: app.globalData.openid,
                    avatar: user.avatarUrl,
                    nickname: user.nickName,
                    sex: user.gender,
                  },
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  success: function (res) {
                    console.log(res)
                    app.globalData.uid = res.data.data.id
                    that.setData({
                      status1: res.data.status
                    })
                    that.index()
                  },
                })
              }
            })
          }
        })

      },
      fail: res => {
        //拒绝授权
        // that.showErrorModal('您拒绝了请求');
        return;
      }
    })

  }),
  getPhoneNumber: function (e) { //点击获取手机号码按钮
    var that = this;
    let encryptedData = e.detail.encryptedData;
    let iv = e.detail.iv;
    console.log(app.globalData.session_key)
    console.log(e.detail.encryptedData)
    console.log(e.detail.iv)
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.request({
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            url: app.globalData.url_l + "api/index/getMobile",
            data: {
              uid: app.globalData.uid,
              sessionKey: app.globalData.session_key,
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv
            },
            success: function (result) {
              console.log(result)
              app.globalData.dh_x = result.data.mobile
              if (result.data.data.shi == 0) {
                wx.navigateTo({
                  url: '../xuan_dz/xuan_dz',
                })
              } else {
                app.globalData.status = 1
              }
              wx.request({
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded' // 默认值
                },
                url: app.globalData.url_l + "api/index/mem_mobile",
                data: {
                  uid: app.globalData.uid,
                  mobile: result.data.mobile
                },
                success: function (result) {
                  console.log(result)

                }
              })
              if (app.globalData.dz == 1) {
                app.globalData.status = 1
                that.setData({
                  isHide: false,
                  dl_yc: "display:none",
                  sq_l: "display:none",
                  kp_xz: "display:block"
                });
              }
              if (result.data == '') {

              } else {
                that.setData({
                  sq_l: "display:none"
                })
              }
            },
            fail() {
              //这是失败函数
            }
          })
        } else {
          console.log("登陆失败！")
        }
      }
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.globalData.shu = 0
    var that = this;
    // 获取用户信息
    wx.login({
      success: function (data) {
        var postData = {
          code: data.code
        };
        console.log(postData)
        wx.request({
          url: app.globalData.url_l + 'api/login/member_open', //注意改成自己的服务器请求地址哦！
          data: postData,
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
          },
          success: function (res) {
            console.log(res)
            that.setData({
              status1: res.data.status
            })
            if (res.data.status == 0) {
              that.index()
            } else if (res.data.status == 1) {
              app.globalData.uid = res.data.data.id
              that.index()
            }
          }
        })
      }
    })

    wx.getSetting({
      success: function (res) {
        //console.log(res)
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
              // 根据自己的需求有其他操作再补充
              // 我这里实现的是在用户授权成功后，调用微信的 wx.login 接口，从而获取code
              wx.login({
                success: res => {
                  //  that.setData({
                  //   sq_l:"display:none"
                  //  })
                  // 可以传给后台，再经过解析获取用户的 openid
                }
              });
            }
          });
        } else {

          // 用户没有授权
          // 改变 isHide 的值，显示授权页面

          that.setData({
            isHide: true,
            // dl_yc:"display:block",
            sq_l: "display:none"
          });
        }
      }
    });

    // 查看是否授权
    //获取用户本地是否是第一次进入新版本
    var versions = wx.getStorageSync('versions');
    if (versions == '1') {
      wx.getSetting({
        success: function (res) {
          if (res.authSetting['scope.userInfo']) {
            //调用共通的登录方法
            app.util.getUserInfo(
              function (userinfo) {
                that.setData({
                  userinfo: userinfo
                })
              });
          } else {
            // 用户没有授权
            // 改变 isHide 的值，显示授权页面
            that.setData({
              isHide: true
            });
          }
        }
      });
    } else {
      // 用户没有授权
      // 改变 isHide 的值，显示授权页面
      that.setData({
        isHide: true
      });
    }
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })
    // wx.qy.login({
    //   success: function(res) {
    //     console.log(res.code)
    //     app.globalData.code_qy = res.code
    //     wx.login({
    //       success: function (data) {
    //         console.log(data.code)
    //         app.globalData.xcx_code = data.code
    //         var postData = {
    //           code: data.code
    //         };
    //         if (data.code) {
    //           //发起网络请求
    //           wx.request({
    //             url: app.globalData.url_l+'api/index/get_code2session',
    //             method: 'get',
    //             data: {
    //               code: app.globalData.code_qy,
    //               xcx_code:app.globalData.xcx_code
    //             },
    //             header: {
    //               'content-type': 'application/x-www-form-urlencoded'
    //             },
    //             success: function (res) {
    //               console.log(res)
    //               app.globalData.sc = res.data.code
    //               app.globalData.sc_ts = res.data.mag
    //               if(res.data.code==2){
    //                 wx.showModal({
    //                   title: '温馨提示',
    //                   content: res.data.mag,
    //                   success (res) {
    //                     if (res.confirm) {
    //                       console.log('用户点击确定')
    //                     } else if (res.cancel) {
    //                       console.log('用户点击取消')
    //                     }
    //                   }
    //                 })
    //               }
    //              app.globalData.sm_rz = res.data.shiming;
    //              app.globalData.sm_qy = res.data.qiye;
    //              app.globalData.userid = res.data.userid;
    //              app.globalData.corpid = res.data.corpid;
    //              app.globalData.openid = res.data.openid;
    //              app.globalData.uid = res.data.id;
    //              app.globalData.code1 = res.data.code;
    //              console.log(app.globalData.uid)
    //              wx.request({
    //               url: app.globalData.url_l + 'api/member/moban_list',
    //               method: 'post',
    //               data:{
    //                   uid:app.globalData.uid,
    //                   state:1,
    //                   shu:0,
    //                   lei:'',
    //                   content:"",
    //               },
    //               header: {
    //                 'content-type': 'application/x-www-form-urlencoded'
    //               },
    //               success: function (res) {
    //                 console.log(res)
    //                 that.setData({
    //                     tui_j:res.data.data
    //                 })
    //               }
    //             })
    //             wx.request({
    //               url: app.globalData.url_l + 'api/member/index',
    //               method: 'post',
    //               data:{
    //                   uid:app.globalData.uid,
    //               },
    //               header: {
    //                 'content-type': 'application/x-www-form-urlencoded'
    //               },
    //               success: function (res) {
    //                 console.log(res)
    //                 console.log(app.globalData.uid)
    //                  if(res.data.code==1){
    //                    app.globalData.corpid = res.data.data.corpid
    //                    app.globalData.sf_zs = res.data.data.idnum
    //                    that.setData({
    //                      ge_xx:res.data.data,
    //                      taxs:res.data
    //                    })
    //                  }else{
    //                   that.setData({
    //                     ge_xx:'',
    //                     taxs:''
    //                   })
    //                  }
    //               }
    //             })
    //             if(app.globalData.code1==1){

    //             }else if(app.globalData.code1==0){
    //               wx.qy.checkSession({
    //                 success: function(res){
    //                   // console.log(res)
    //                    wx.qy.getEnterpriseUserInfo ({
    //                       success: function(res) {
    //                         console.log(res)
    //                         var name = res.userInfo.name
    //                         var gender = res.userInfo.gender
    //                         that.setData({
    //                           name:res.userInfo
    //                         })
    //                         wx.qy.getAvatar ({
    //                           success: function(res) {
    //                             console.log(res)
    //                             console.log("-------------")
    //                             console.log(res.avatar)
    //                             var avatar = res.avatar
    //                             wx.request({
    //                               url: app.globalData.url_l + 'api/index/mem_login',
    //                               method: 'post',
    //                               data:{
    //                                 name:name,
    //                                 gender:gender,
    //                                 avatar:avatar,
    //                                 userid:app.globalData.userid,
    //                                 corpid:app.globalData.corpid,
    //                                 openid:app.globalData.openid,
    //                               },
    //                               header: {
    //                                 'content-type': 'application/x-www-form-urlencoded'
    //                               },
    //                               success: function (res) {
    //                                 console.log(res)
    //                                 wx.request({
    //                                   url: app.globalData.url_l + 'api/member/index',
    //                                   method: 'post',
    //                                   data:{
    //                                       uid:app.globalData.uid,
    //                                   },
    //                                   header: {
    //                                     'content-type': 'application/x-www-form-urlencoded'
    //                                   },
    //                                   success: function (res) {
    //                                     console.log(res)
    //                                     console.log(app.globalData.uid)
    //                                      if(res.data.code==1){
    //                                        app.globalData.corpid = res.data.data.corpid
    //                                        app.globalData.sf_zs = res.data.data.idnum
    //                                        that.setData({
    //                                          ge_xx:res.data.data,
    //                                          taxs:res.data
    //                                        })
    //                                      }else{
    //                                       that.setData({
    //                                         ge_xx:'',
    //                                         taxs:''
    //                                       })
    //                                      }
    //                                   }
    //                                 })

    //                               }
    //                             })
    //                             that.setData({
    //                               avatar:res
    //                             })
    //                           },
    //                           fail: function(res) {
    //                             console.log(res.fail_reason)
    //                           }
    //                         })
    //                       }
    //                     })
    //                 },
    //                 fail: function(){
    //                   // session_key 已经失效，需要重新执行登录流程
    //                   wx.qy.login() //重新登录
    //                 }
    //               })
    //             }


    //             }
    //           })
    //         } else {
    //           console.log('登录失败！' + res.errMsg)
    //         }
    //       }
    //     })

    //   }
    // });
    wx.getSystemInfo({
      success(res) {
        console.log(res)
        if (res.environment == 'wxwork') {

        } else {
          //  wx.reLaunch({
          //    url: '../tishi/tishi',
          //  })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})